/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sigmacinema.ui.tapestry.controller;

import com.javaforge.tapestry.spring.annotations.InjectSpring;
import java.util.ArrayList;
import java.util.List;
import org.apache.tapestry.annotations.Bean;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.apache.tapestry.form.StringPropertySelectionModel;
import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.record.PropertyChangeObserver;
import org.apache.tapestry.valid.ValidationDelegate;
import sigmacinema.entity.Hall;
import sigmacinema.entity.Movie;
import sigmacinema.entity.Schedule;
import sigmacinema.service.SigmaCinemaService;

/**
 *
 * @author sangaji
 */
public abstract class ScheduleController extends BasePage{

    @InjectSpring("SigmaCinemaService")
    public abstract SigmaCinemaService getSigmaCinemaService();
    @Bean
    public abstract ValidationDelegate getDelegate();
    
    //@InjectPage("ChooseSeat")
    //public abstract HomeService getChooseSeat();

    private List<Movie> listMovie = new ArrayList<Movie>();
    private List<Schedule> listSchedule = new ArrayList<Schedule>();
    private List<Hall> listHall = new ArrayList<Hall>();
    private Schedule schedule = new Schedule();
    private Movie movie = new Movie();
    private Hall hall = new Hall();

    public Hall getHall() {
        return hall;
    }

    public void setHall(Hall hall) {
        this.hall = hall;
    }

    public Movie getMovie() {
        return movie;
    }

    public void setMovie(Movie movie) {
        this.movie = movie;
    }

    public Schedule getSchedule() {
        return schedule;
    }

    public void setSchedule(Schedule schedule) {
        this.schedule = schedule;
    }

    public List<Schedule> getListSchedule() {
        listSchedule = getSigmaCinemaService().findAllSchedule();
        return listSchedule;
    }

    public void setListSchedule(List<Schedule> listSchedule) {
        this.listSchedule = listSchedule;
    }
    
    public String onClickDetailsLink(Integer id){
        schedule = getSigmaCinemaService().findScheduleById(id);
        return "member/ChooseSeat";
    }
    
    public IPropertySelectionModel getMovieList() {
        listMovie = getSigmaCinemaService().FindAllMovie();
        String[] movieId = new String[listMovie.size()];
        for(int i=0;i<listMovie.size();i++){
            movieId[i] = listMovie.get(i).getId().toString();
        }
        return new StringPropertySelectionModel(movieId);
        //return new StringPropertySelectionModel(new String[]{"Action", "Comedy","Drama","Horror"});
    }
    
    public IPropertySelectionModel getHallList() {
        listHall = getSigmaCinemaService().FindAllHall();
        String[] hallId = new String[listHall.size()];
        for(int i=0;i<listHall.size();i++){
            hallId[i] = listHall.get(i).getId().toString();
        }
        return new StringPropertySelectionModel(hallId);
        //return new StringPropertySelectionModel(new String[]{"Action", "Comedy","Drama","Horror"});
    }
    
    public String save(){
        ValidationDelegate delegate = getDelegate();
        if(delegate.getHasErrors()){
           return null;            
        }else{
            movie = getSigmaCinemaService().FindMovieByKode(movie.getId());
            hall = getSigmaCinemaService().findHallById(hall.getId());
            schedule.setMovie(movie);
            schedule.setHall(hall);
            schedule.setShowSchedule(true);
            getSigmaCinemaService().saveSchedule(schedule);
            schedule = new Schedule();
        }
        return null;
    }
    
    @Override
    public String getClientId() {
        return null;
    }

    @Override
    public void setClientId(String string) {
        
    }

    @Override
    public PropertyChangeObserver getPropertyChangeObserver() {
        return null;
    }
    
}
